package com.bestcem.xm.user.mq.send.biz.impl;

import com.alibaba.fastjson.JSONObject;
import com.bestcem.xm.common.core.config.mq.XmMessageProducer;
import com.bestcem.xm.common.core.constant.StreamMqBinderConstants;
import com.bestcem.xm.user.mq.info.group.GroupNameInfo;
import com.bestcem.xm.user.mq.info.group.StoreInfo;
import com.bestcem.xm.user.mq.message.group.ChangeGroupCodeMessage;
import com.bestcem.xm.user.mq.message.group.ChangeGroupLevelNameMessage;
import com.bestcem.xm.user.mq.message.user.CreateUserMessage;
import com.bestcem.xm.user.mq.message.user.UpdateUserMessage;
import com.bestcem.xm.user.mq.send.biz.UserMessageSendService;
import com.fasterxml.jackson.core.JsonProcessingException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.bestcem.xm.common.core.constant.StreamMqBinderConstants.*;

@Slf4j
@Service
public class UserMessageSendServiceImpl implements UserMessageSendService {

    private XmMessageProducer messageProducer;

    @Autowired
    public void setMessageProducer(XmMessageProducer messageProducer) {
        this.messageProducer = messageProducer;
    }

    @Override
    public void publishGroupValidateList(String orgId, List<String> groupIds) {
        JSONObject data = new JSONObject();
        data.put("org_id", orgId);
        data.put("id_list", groupIds);
        try {
            messageProducer.send(StreamMqBinderConstants.USER_GROUP_VALIDATE_LIST, data);
        } catch (JsonProcessingException e) {
            log.error("[user]组织恢复失效:{},{}", data.toJSONString(), e.getMessage());
        }
    }

    @Override
    public void publishGroupTitleModifyList(String orgId, List<GroupNameInfo> title_modify) {
        //todo 需实现消费
        JSONObject data = new JSONObject();
        data.put("org_id", orgId);
        data.put("title_modify_list", title_modify);
        try {
            messageProducer.send(StreamMqBinderConstants.USER_GROUP_TITLE_MODIFY_LIST, data);
        } catch (JsonProcessingException e) {
            log.error("[user]组织名称批量变更:{},{}", data.toJSONString(), e.getMessage());
        }
    }

    @Override
    public void publishGroupCodeModify(ChangeGroupCodeMessage message) {
        //todo 需实现消费
        try {
            messageProducer.send(StreamMqBinderConstants.USER_GROUP_CODE_MODIFY, message);
        } catch (JsonProcessingException e) {
            log.error("[user]组织编码变更:{},{}", message.toString(), e.getMessage());
        }
    }

    @Override
    public void publishCreateStoreList(String orgId, String root_id, List<StoreInfo> node_list) {
        //todo 需实现消费
        JSONObject data = new JSONObject();
        data.put("org_id", orgId);
        data.put("node_list", node_list);
        try {
            messageProducer.send(StreamMqBinderConstants.USER_NEW_STORE_LIST, data);
        } catch (JsonProcessingException e) {
            log.error("[user]新增门店组织列表：【门店是表示处于最后一个层级的组织】:{},{}", data.toJSONString(), e.getMessage());
        }

    }

    @Override
    public void publishGroupLevelDelete(String orgId, String lv_id) {
        //todo 需实现消费
        JSONObject data = new JSONObject();
        data.put("org_id", orgId);
        data.put("id", lv_id);
        try {
            messageProducer.send(StreamMqBinderConstants.USER_GROUPLEVEL_DELETED, data);
        } catch (JsonProcessingException e) {
            log.error("[user]删除层级:{},{}", data.toJSONString(), e.getMessage());
        }

    }

    @Override
    public void publishGroupInvalidateList(String orgId, List<String> del_grp_id_list) {
        JSONObject data = new JSONObject();
        data.put("org_id", orgId);
        data.put("id_list", del_grp_id_list);
        try {
            messageProducer.send(StreamMqBinderConstants.USER_GROUP_INVALIDATE, data);
        } catch (JsonProcessingException e) {
            log.error("[user]层级失效:{},{}", data.toJSONString(), e.getMessage());
        }
    }

    @Override
    public void publishGroupsChange(String orgId, String rootGroupId) {
        JSONObject data = new JSONObject();
        data.put("org_id", orgId);
        data.put("root_group_id", rootGroupId);
        try {
            messageProducer.send(StreamMqBinderConstants.USER_GROUPS_CHANGED, data);
        } catch (JsonProcessingException e) {
            log.error("[user]组织发生变化:{},{}", data.toJSONString(), e.getMessage());
        }
    }

    @Override
    public void userOrgCreate(String orgId) {
        Map<String, Object> data = new HashMap<>();
        data.put("id", orgId);
        try {
            messageProducer.send(StreamMqBinderConstants.USER_ORG_CREATE, data);
        } catch (JsonProcessingException e) {
            log.error("[user] userOrgCreate:{},{}", data, e.getMessage());
        }
    }

    @Override
    public void publishGroupLevelTitleModify(String orgId, String groupLevelId, String oldGroupLevelName, String newGroupLevelName) {
        ChangeGroupLevelNameMessage dto = new ChangeGroupLevelNameMessage(orgId, groupLevelId, oldGroupLevelName, newGroupLevelName);
        try {
            messageProducer.send(StreamMqBinderConstants.USER_GROUPLEVEL_TITLE_MODIFY, dto);
        } catch (JsonProcessingException e) {
            log.error("[user] publishGroupLevelTitleModify:{},{}", dto, e.getMessage());
        }
    }

    @Override
    public void publishSmtpDelete(List<String> smtpIds) {
        try {
            messageProducer.send(USER_SMTP_DELETED, smtpIds);
        } catch (JsonProcessingException e) {
            log.error("[User] publishSmtpDelete发送mq消息失败", e);
        }
    }

    @Override
    public void publishUserCreate(String userId, List<String> groupIds) {
        try {
            CreateUserMessage message = new CreateUserMessage(userId, groupIds);
            messageProducer.send(USER_CREATED_OUT, message);
        } catch (JsonProcessingException e) {
            log.error("[User] publishUserCreate发送mq消息失败", e);
        }
    }

    @Override
    public void publishUserUpdate(String userId, List<String> groupIds) {
        try {
            UpdateUserMessage message = new UpdateUserMessage(userId, groupIds);
            messageProducer.send(USER_UPDATED_OUT, message);
        } catch (JsonProcessingException e) {
            log.error("[User] publishUserUpdate发送mq消息失败", e);
        }
    }
}
